package com.abe.mvp.view.adapter;

import java.io.File;
import java.lang.reflect.Type;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.abe.app.RelayoutTool;
import com.abe.mvp.model.bean.CardReportBean;
import com.abe.mvp.view.activity.ExerciseReportActivity;
import com.abe.threadmill.R;
import com.squareup.picasso.Picasso;

@SuppressLint({ "InflateParams", "ClickableViewAccessibility" })
public class ExerciseReportAdapter extends BaseAdapter {

	private List<CardReportBean> data;
	private LayoutInflater layoutInflater;
	private float scale;
	private Context context;

	public ExerciseReportAdapter(Context context, List<CardReportBean> data,
			float scale) {
		this.data = data;
		this.scale = scale;
		this.context = context;
		this.layoutInflater = LayoutInflater.from(context);
	}

	public final class ViewHolder {
		public LinearLayout layout;
		public TextView name;
		public ImageView image;
	}

	public final class ReportHolder {
		public LinearLayout layout;
		public TextView name;
		public TextView num;
		public TextView unit;
		public ImageView image;
	}

	@Override
	public int getCount() {
		return data.size();
	}

	/**
	 * 获得某一位置的数
	 */
	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	/**
	 * 获得唯一标识
	 */
	@Override
	public long getItemId(int position) {
		return position;
	}

	private void setDrawable(ImageView imageView, Object load, Type type) {

		if (type.equals(String.class)) {
			Picasso.with(context).load((String) load).into(imageView);
		} else if (type.equals(Uri.class)) {
			Picasso.with(context).load((Uri) load).into(imageView);
		} else if (type.equals(File.class)) {
			Picasso.with(context).load((File) load).into(imageView);
		} else if (type.equals(Integer.class)) {
			Picasso.with(context).load((Integer) load).into(imageView);
		}
	}

	private View initReportMenu(final int position, View convertView,
			ViewGroup parent) {
		ReportHolder holder = null;
		if (convertView == null) {
			holder = new ReportHolder();
			// 获得组件，实例化组件
			convertView = layoutInflater.inflate(R.layout.item_list_report,
					null);
			RelayoutTool.relayoutViewHierarchy(convertView, scale);
			holder.layout = (LinearLayout) convertView
					.findViewById(R.id.item_report_layout);
			holder.name = (TextView) convertView
					.findViewById(R.id.item_report_name);
			holder.num = (TextView) convertView
					.findViewById(R.id.item_report_num);
			holder.unit = (TextView) convertView
					.findViewById(R.id.item_report_unit);
			holder.image = (ImageView) convertView
					.findViewById(R.id.item_report_image);
			convertView.setTag(holder);
		} else {
			holder = (ReportHolder) convertView.getTag();
		}

		CardReportBean bean = data.get(position);
		holder.name.setText(bean.getName());
		holder.num.setText(bean.getNum());
		holder.unit.setText(bean.getUnit());
		setDrawable(holder.image, bean.getDrawable(), bean.getType());
		return convertView;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		if (context.getClass().equals(ExerciseReportActivity.class)) {
			convertView = initReportMenu(position, convertView, parent);
		}
		return convertView;
	}

}
